package com.example.martian.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class NewSendEmail {

    /**
     * 邮件工具类
     * 功能：
     * 完成对给定的邮箱，发送邮件
     */
    public static class MailUtils extends Thread {
        /**
         * 向特定邮箱发送邮件
         *
         * @param email 收件邮箱
         * @param code  验随机证码
         */
        private String email;
        private String code;

        public MailUtils(String email, String code) {
            this.email = email;
            this.code = code;
        }

        public void run() {
            sendMailForResetPwd();
        }

        public void sendMailForResetPwd() {

            String sendMember = "graycloud2021@163.com";
            String smtpHost = "smtp.163.com";
//            String smtpPort = "994";
            String smtpPort = "465";
            String smtpMember = "graycloud2021@163.com";
            String smtpPassword = "TKYTOLLMILADRLXV";
            Properties properties = new Properties();
            properties.put("mail.transport.protocol", "smtp");// 连接协议
            properties.put("mail.smtp.host", smtpHost);// 主机名
            properties.put("mail.smtp.port", smtpPort);// 端口号
            properties.put("mail.smtp.auth", "true");   // 是否进行身份验证 (默认false)
            properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接（若服务器未配置则false，否则会报错）
            properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息

            try {
                //QQ邮箱需要下面这段代码，163邮箱不需要
                /*MailSSLSocketFactory sf = new MailSSLSocketFactory();
                sf.setTrustAllHosts(true);
                properties.put("mail.smtp.ssl.enable", "true");
                properties.put("mail.smtp.ssl.socketFactory", sf);*/
                // 得到会话对象
                Session session = Session.getInstance(properties);
                // 获取邮件对象
                Message message = new MimeMessage(session);
                // 设置发件人邮箱地址
                message.setFrom(new InternetAddress(sendMember));
                // 设置收件人邮箱地址
//            message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(toEmail),new InternetAddress("test@test.com")});  // 多个收件人
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(email)); //单个收件人
                // 设置邮件标题
                message.setSubject("验证码");

                // 设置邮件内容
                String content = "<html><head></head><body><h1>邮箱验证</h1><h3>【灰度云矿】您的验证码是 " + code + ",５分钟内有效。若非本人操作请忽略此消息  [请勿回复]</h3></body></html>";
                message.setContent(content, "text/html;charset=utf-8");

                Transport tr = session.getTransport("smtp");
                tr.connect(smtpHost, smtpMember, smtpPassword);   // 关键（连接发件服务器smtp,传入参数：主机名，连接账户，连接密码）

                message.saveChanges();   // 保存并生成最终的邮件内容
                tr.sendMessage(message, message.getAllRecipients());
                tr.close();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {

        MailUtils mailUtils = new MailUtils("531961985@qq.com","612465");
        mailUtils.run();
    }
}

